package com.iwords.api.web.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import com.iwords.constants.Constants;
import com.iwords.service.object.ExamItemsEntity;
import com.iwords.api.web.base.BaseAction;

/**
 * @author       BOLIN.HBC 
 * @time         2011-4-5    
 * @description  试卷-SERVLET       
 */
@WebServlet(name = "examItemAction", urlPatterns = { "/examItem" })
public class ExamItemAction extends BaseAction<ExamItemsEntity> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doProcess(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException, JSONException {
		String action = req.getParameter("action");
		if (Constants.EAMITEMS_LIST_ACTION.equals(action)) {
			doGetItemsList(req, res);
		} else if (Constants.EAMITEMS_SINGLE_ACTION.equals(action)) {

		}

	}

	public void doGetItemsList(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException, JSONException {
		JSONObject result = new JSONObject();
		result.put(Constants.IS_SUCCESS, Boolean.TRUE);
		String wordid = req.getParameter("wordid");
		if (wordid == null) {
			result.put(Constants.IS_SUCCESS, Boolean.FALSE);
			result.put(Constants.MESSAGE, "wordid can't be null");
			jsonPrint(res, result);
			return;
		}

		List<ExamItemsEntity> resultList = new ArrayList<ExamItemsEntity>();

		// 根据单词ID查询出所有题目

		com.iwords.utils.JSONObject defaultValue = new com.iwords.utils.JSONObject();
		defaultValue.putColumn("itemid", "examid", "itemno", "itemname", "itemtype", "itemcontent", "audio", "createtime", "createtime");

		for (ExamItemsEntity item : resultList) {
			defaultValue.putValue(item.getItemid(), item.getExamid(), item.getItemno(), item.getItemname(), item.getItemtype(), item.getItemcontent(),
					item.getAudio(), item.getCreatetime().getTime(), item.getUpdatetime().getTime());
		}
		result.put(Constants.DEFAULT_VALUE, defaultValue);
		jsonPrint(res, result);
	}

	public ExamItemsEntity initEntity(HttpServletRequest req) {

		return null;
	}

}